#include <cstdlib>
#include <sstream>
#include <osg/io_utils>
#include <osg/ArgumentParser>
#include <osg/Geode>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgText/Text>



osg::Camera* createOrthoCamera(double width, double height)
{
	osg::Camera* camera = new osg::Camera();

	camera->getOrCreateStateSet()->setMode(
		GL_LIGHTING,
		osg::StateAttribute::PROTECTED | osg::StateAttribute::OFF
	);

	osg::Matrix m = osg::Matrix::ortho2D(0.0f, width, 0.0f, height);

	camera->setProjectionMatrix(m);
	camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
	camera->setViewMatrix(osg::Matrix::identity());
	camera->setClearMask(GL_DEPTH_BUFFER_BIT);
	camera->setRenderOrder(osg::Camera::POST_RENDER);

	return camera;
}

osgText::Text* createLabel(const std::string& l, const char* f, unsigned int size)
{
	static osg::Vec3 pos(300.0f, 500.0f, 0.0f);

	osgText::Text* label = new osgText::Text();
	osgText::Font* font  = osgText::readFontFile(f);

	label->setFont(font);
	label->setCharacterSize(size);
	label->setFontResolution(size, size);
	label->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
	label->setPosition(pos);
	label->setAlignment(osgText::Text::LEFT_BOTTOM);

	label->setText(l);
	pos.y() += size + 10.0f;

	return label;
}

typedef std::list<unsigned int> Sizes;

int main(int argc, char** argv)
{
	osgViewer::Viewer   viewer;
	
	viewer.addEventHandler(new osgViewer::StatsHandler());
	viewer.addEventHandler(new osgViewer::WindowSizeHandler());

	osg::Group*  group  = new osg::Group();
	osg::Camera* camera = createOrthoCamera(1280.0f, 1024.0f);


	osg::Geode* geode = new osg::Geode();
	std::stringstream ss;
	ss << "TEST OSG FONT [size " << 50 <<"] [font arial]";

	geode->addDrawable(createLabel(ss.str(), "data/font/arial.ttf", 50));
	camera->addChild(geode);
	group->addChild(camera);
	viewer.setSceneData(group);
	return viewer.run();
}

